/**
 * 作者:TL(tongling@datoubao.com)
 * 时间：2020-07-23.
 * 功能：
 */

/**
 * 知识管理添加页面
 */
layui.use(['tree', 'layer', 'laypage', 'element', 'layedit'], function () {
    var layer = layui.layer
        , laypage = layui.laypage
        , element = layui.element
        , layedit = layui.layedit
        , $ = layui.jquery;
    $(function(){
        tinyMCE.editors[0].setContent("<p>标题：</p>\n" +
            "<hr />\n" +
            "<p>正文：</p>");
    })

    // 目录管理分页
    laypage.render({
        elem: 'yuanziListPage'
        ,count: 100
        // ,first: false
        // ,last: false
        // ,prev: '<em><</em>'
        // ,next: '<em>></em>'
        ,jump: function(obj){
            console.log(obj)
            // 当前页点击
            var currentPage = obj.curr;
            var resultData = [
                {
                    id:0
                    ,name:'原子1'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:1
                    ,name:'原子2'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:2
                    ,name:'原子3'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:3
                    ,name:'原子4'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:4
                    ,name:'原子5'
                    ,show:false
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:5
                    ,name:'原子6'
                    ,show:false
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:6
                    ,name:'原子7'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:7
                    ,name:'原子8'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:8
                    ,name:'原子9'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
                ,{
                    id:9
                    ,name:'原子10'
                    ,show:true
                    ,time:'2019.11.24 13:00'
                    ,creater:'张东阳'
                    ,createrNo:'D483924'
                }
            ]
            var resultItemsTemp = ''
            for(var i = 0;i<resultData.length;i++){
                resultItemsTemp += '<tr data-id="'+resultData[i].id+'">\n' +
                    '  <td>'+resultData[i].name+'</td>\n' +
                    '  <td>'+resultData[i].creater+'</td>\n' +
                    '  <td>'+resultData[i].createrNo+'</td>\n' +
                    '  <td>'+resultData[i].time+'</td>\n' +
                    '  <td>\n'+
                    '    <div class="yuanzi-number pull-left">'+resultData[i].id+'</div>\n'+
                    '    <dl class="pull-right">\n'+
                    '       <dt><button type="button" class="btn btn-link uparrow-btn"><span class="uparrow"></span></button></dt>\n'+
                    '       <dd><button type="button" class="btn btn-link downarrow-btn"><span class="downarrow"></span></button></dd>\n'+
                    '    </dl>\n'+
                    '  </td>\n'+
                    '  <td>\n' +
                    '     <a href="javascript:void(0)" class="edit-btn"><img src="../../image/detail.png" alt=""></a>'+
                    '     <a href="javascript:void(0)" class="delete-btn"><img src="../../image/delete.png" alt=""></a>'+
                    '  </td>\n' +
                    '  </tr>'
            }
            $("#yuanziList").html(resultItemsTemp)
        }
    });
    // 删除管理知识
    $(document).on("click",".dele-about",function(){
        $(this).parents("li").remove();
        $("#aboutknow-items").find("li").each(function(){
            $(this).find(".num").html(($(this).index()+1)+".");
        })
    })

    // 原子表格编辑删除按钮
    $(document).on("click",".edit-btn",function(){
        var index =$(this).parents("tr").data("id");
        layer.msg("您点击了id为" +index+"编辑按钮")
    })
    $(document).on("click",".delete-btn",function(){
        var index =$(this).parents("tr").data("id");
        layer.msg("您点击了id为" +index+"删除按钮")
    })
    // 原子表格向上、向下箭头点击
    $(document).on("click",".uparrow",function(){
        var index =$(this).parents("tr").data("id");
        layer.msg("您点击了id为" +index+"的向上排序按钮")
    })
    $(document).on("click",".downarrow",function(){
        var index =$(this).parents("tr").data("id");
        layer.msg("您点击了id为" +index+"的向下排序按钮")
    })
    // 打开关联知识弹层
    $("#addAboutKnow").on("click",function(event){
        event.preventDefault();
        // 生成左侧菜单
        layui.tree({
            elem: '#relaTree' //指定元素
            ,target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
            ,click: function(item){ //点击节点回调
                layer.msg('当前节名称：'+ item.name + '<br>全部参数：'+ JSON.stringify(item));
                console.log(item);
            }
            ,nodes: [ //节点
                {
                    name: '国级'
                    ,id: 1
                    // ,alias: 'changyong'
                    ,spread: true
                    ,children: [
                        {
                            name: '增值税'
                            ,id: 11
                            // ,href: '#'
                            ,spread: true
                            ,children:[
                                {
                                    name: '房地产'
                                    ,id: 111
                                    ,spread: true
                                    ,children:[
                                        {
                                            name: '房地产知识点'
                                            ,id: 1111
                                        }, {
                                            name: '发票'
                                            ,id: 1112
                                        }, {
                                            name: '金融'
                                            ,id: 1113
                                        }, {
                                            name: '文件库'
                                            ,id: 1113
                                        }
                                    ]
                                }
                                ,{
                                    name: '发票'
                                    ,id: 112
                                }
                                ,{
                                    name: '金融'
                                    ,id: 113
                                }
                                ,{
                                    name: '文件库'
                                    ,id: 114
                                    ,children:[
                                        {
                                            name: '文件库知1'
                                            ,id: 1141
                                        }, {
                                            name: '文件库知2'
                                            ,id: 1142
                                        }, {
                                            name: '文件库知3'
                                            ,id: 1143
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            name: '个人所得税'
                            ,id: 12
                            // ,href: '#'
                            ,children:[
                                {
                                    name: '增值税'
                                    ,id: 121
                                    ,children:[
                                        {
                                            name: '房地产'
                                            ,id: 1211
                                        }, {
                                            name: '发票'
                                            ,id: 1212
                                        }, {
                                            name: '金融'
                                            ,id: 1213
                                        }, {
                                            name: '文件库'
                                            ,id: 1213
                                        }
                                    ]
                                }, {
                                    name: '发票'
                                    ,id: 112
                                }, {
                                    name: '金融'
                                    ,id: 113
                                }, {
                                    name: '文件库'
                                    ,id: 114
                                    ,children:[
                                        {
                                            name: '文件库知1'
                                            ,id: 1141
                                        }, {
                                            name: '文件库知2'
                                            ,id: 1142
                                        }, {
                                            name: '文件库知3'
                                            ,id: 1143
                                        }
                                    ]
                                }
                            ]
                        },
                    ]
                }
            ]
        });
        $("#aboutknowModal").modal("show");
    })
    // 添加原子按钮
    $(document).on("click",".addyuanzi",function(){
        $("#newyuanziModal").modal("show");
    })
    // 保存关联关系弹层
    $(".saveRelaknowModel").on("click",function(){
        var checkedNum = $("input[name=relaknow]:checked").val();
        layer.alert( "您选择了："+checkedNum);

        var tempHtm = '<li>' +
            '<span class="num"></span>' +
            '<span class="itemName">关联知识</span> ' +
            '<span class="iconfont icon-off pull-right dele-about"></span>' +
            '</li>'
        $("#aboutknow-items").append(tempHtm);
        $("#aboutknow-items").find("li").each(function(){
            $(this).find(".num").html(($(this).index()+1)+".");
        })

        setTimeout(function () {
            $("#aboutknowModal").modal("hide");
        },3000)
    })
    // 保存原子弹层信息
    $(".saveyuanziModel").on("click",function(){
        var yuanziName = $("#yuanziInput1").val();//原子名称
        var keyWord = $("#yuanziInput2").val();//关键词
        var yuanziFile = $("#yuanzi-uploadfile").val();//原子附件
        var yuanziDetail = tinyMCE.editors[1].getContent();//原子详情
        layer.alert( yuanziName+";\r"+keyWord+ ";\r" +yuanziFile+";\r" +yuanziDetail)

        setTimeout(function () {
            $("#newyuanziModal").modal("hide");
        },5000)
    })
    // 保存整个页面信息
    $(".rightsaveBtn").on("click",function(){
        // 获取的富文本信息
        layer.msg(  tinyMCE.editors[0].getContent());
    })


});

// 富文本初始化
tinymce.init({
    selector: '.tinymceEditor',
    //skin:'oxide-dark',
    language:'zh_CN',
    plugins: 'print preview searchreplace autolink directionality visualblocks visualchars fullscreen image link template code codesample table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists wordcount imagetools textpattern help emoticons autosave autoresize lineheight',
    // toolbar: 'code undo redo restoredraft | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link anchor | alignleft aligncenter alignright alignjustify outdent indent | \
    //                  styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | \
    //                  table image charmap emoticons hr pagebreak insertdatetime print preview | fullscreen | lineheight',
    toolbar: ' forecolor backcolor bold italic underline strikethrough link | alignleft aligncenter alignright alignjustify  lineheight | table image insertdatetime  preview \
                     | fullscreen | ',
    menubar: false,
    height: 650, //编辑器高度
    min_height: 200,
    max_height:400,
    /*content_css: [ //可设置编辑区内容展示的css，谨慎使用
        '/static/reset.css',
        '/static/ax.css',
        '/static/css.css',
    ],*/
    fontsize_formats: '12px 14px 16px 18px 24px 36px 48px 56px 72px',
    font_formats: '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;知乎配置=BlinkMacSystemFont, Helvetica Neue, PingFang SC, Microsoft YaHei, Source Han Sans SC, Noto Sans CJK SC, WenQuanYi Micro Hei, sans-serif;小米配置=Helvetica Neue,Helvetica,Arial,Microsoft Yahei,Hiragino Sans GB,Heiti SC,WenQuanYi Micro Hei,sans-serif',
    /*link_list: [
        { title: '预置链接1', value: 'http://www.tinymce.com' },
        { title: '预置链接2', value: 'http://tinymce.ax-z.cn' }
    ],
    image_list: [
        { title: '预置图片1', value: 'https://www.tiny.cloud/images/glyph-tinymce@2x.png' },
        { title: '预置图片2', value: 'https://www.baidu.com/img/bd_logo1.png' }
    ],*/
    /* image_class_list: [
        { title: 'None', value: '' },
        { title: 'Some class', value: 'class-name' }
    ],*/
    //importcss_append: true,
    //自定义文件选择器的回调内容
    file_picker_callback: function (callback, value, meta) {
        if (meta.filetype === 'file') {
            callback('https://www.baidu.com/img/bd_logo1.png', { text: 'My text' });
        }
        if (meta.filetype === 'image') {
            callback('https://www.baidu.com/img/bd_logo1.png', { alt: 'My alt text' });
        }
        if (meta.filetype === 'media') {
            callback('movie.mp4', { source2: 'alt.ogg', poster: 'https://www.baidu.com/img/bd_logo1.png' });
        }
    },
    //为内容模板插件提供预置模板
    templates: [
        { title: '模板1', description: '介绍文字1', content: '模板内容' },
        { title: '模板2', description: '介绍文字2', content: '<div class="mceTmpl"><span class="cdate">CDATE</span>，<span class="mdate">MDATE</span>，我的内容</div>' }
    ],
    //content_security_policy: "script-src *;",
    extended_valid_elements:'script[src]',
    //
    template_cdate_format: '[CDATE: %m/%d/%Y : %H:%M:%S]',
    template_mdate_format: '[MDATE: %m/%d/%Y : %H:%M:%S]',
    autosave_ask_before_unload: false,
    toolbar_mode : 'wrap',
    // images_upload_base_path: '/demo',
    images_upload_handler: function (blobInfo, succFun, failFun) {
        const formData = new FormData();
        formData.append('file', blobInfo.blob());
        // 上传后台返回
        succFun('https://www.baidu.com/img/flexible/logo/pc/result@2.png');
    },
    //icons:'ax-color',

});

// 上传文件
function getFilePath(type){
    if(type === 1){
        $("#uploadFile").parents(".form-group").find(".showTip").html("已选择文件")
        alert("左侧知识添加附件");
    }else{
        $("#yuanzi-uploadfile").parents(".form-group").find(".showTip").html("已选择文件")
        alert("新增原子添加附件")
    }
}